home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / popen2.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  237 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Spawn a command with pipes to its stdin, stdout, and optionally stderr.
  5.  
  6. The normal os.popen(cmd, mode) call spawns a shell command and provides a
  7. file interface to just the input or output of the process depending on
  8. whether mode is 'r' or 'w'.  This module provides the functions popen2(cmd)
  9. and popen3(cmd) which return two or three pipes to the spawned command.
  10. """
  11. import os
  12. import sys
  13. import warnings
  14. warnings.warn('The popen2 module is deprecated.  Use the subprocess module.', DeprecationWarning, stacklevel = 2)
  15. __all__ = [
  16.     'popen2',
  17.     'popen3',
  18.     'popen4']
  19.  
  20. try:
  21.     MAXFD = os.sysconf('SC_OPEN_MAX')
  22. except (AttributeError, ValueError):
  23.     MAXFD = 256
  24.  
  25. _active = []
  26.  
  27. def _cleanup():
  28.     for inst in _active[:]:
  29.         if inst.poll(_deadstate = sys.maxint) >= 0:
  30.             
  31.             try:
  32.                 _active.remove(inst)
  33.             except ValueError:
  34.                 pass
  35.             except:
  36.                 None<EXCEPTION MATCH>ValueError
  37.             
  38.  
  39.         None<EXCEPTION MATCH>ValueError
  40.     
  41.  
  42.  
  43. class Popen3:
  44.     '''Class representing a child process.  Normally, instances are created
  45.     internally by the functions popen2() and popen3().'''
  46.     sts = -1
  47.     
  48.     def __init__(self, cmd, capturestderr = False, bufsize = -1):
  49.         """The parameter 'cmd' is the shell command to execute in a
  50.         sub-process.  On UNIX, 'cmd' may be a sequence, in which case arguments
  51.         will be passed directly to the program without shell intervention (as
  52.         with os.spawnv()).  If 'cmd' is a string it will be passed to the shell
  53.         (as with os.system()).   The 'capturestderr' flag, if true, specifies
  54.         that the object should capture standard error output of the child
  55.         process.  The default is false.  If the 'bufsize' parameter is
  56.         specified, it specifies the size of the I/O buffers to/from the child
  57.         process."""
  58.         _cleanup()
  59.         self.cmd = cmd
  60.         (p2cread, p2cwrite) = os.pipe()
  61.         (c2pread, c2pwrite) = os.pipe()
  62.         if capturestderr:
  63.             (errout, errin) = os.pipe()
  64.         
  65.         self.pid = os.fork()
  66.         if self.pid == 0:
  67.             os.dup2(p2cread, 0)
  68.             os.dup2(c2pwrite, 1)
  69.             if capturestderr:
  70.                 os.dup2(errin, 2)
  71.             
  72.             self._run_child(cmd)
  73.         
  74.         os.close(p2cread)
  75.         self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
  76.         os.close(c2pwrite)
  77.         self.fromchild = os.fdopen(c2pread, 'r', bufsize)
  78.         if capturestderr:
  79.             os.close(errin)
  80.             self.childerr = os.fdopen(errout, 'r', bufsize)
  81.         else:
  82.             self.childerr = None
  83.  
  84.     
  85.     def __del__(self):
  86.         self.poll(_deadstate = sys.maxint)
  87.         if self.sts < 0:
  88.             if _active is not None:
  89.                 _active.append(self)
  90.             
  91.         
  92.  
  93.     
  94.     def _run_child(self, cmd):
  95.         if isinstance(cmd, basestring):
  96.             cmd = [
  97.                 '/bin/sh',
  98.                 '-c',
  99.                 cmd]
  100.         
  101.         os.closerange(3, MAXFD)
  102.         
  103.         try:
  104.             os.execvp(cmd[0], cmd)
  105.         finally:
  106.             os._exit(1)
  107.  
  108.  
  109.     
  110.     def poll(self, _deadstate = None):
  111.         """Return the exit status of the child process if it has finished,
  112.         or -1 if it hasn't finished yet."""
  113.         if self.sts < 0:
  114.             
  115.             try:
  116.                 (pid, sts) = os.waitpid(self.pid, os.WNOHANG)
  117.                 if pid == self.pid:
  118.                     self.sts = sts
  119.             except os.error:
  120.                 if _deadstate is not None:
  121.                     self.sts = _deadstate
  122.                 
  123.             except:
  124.                 _deadstate is not None
  125.             
  126.  
  127.         None<EXCEPTION MATCH>os.error
  128.         return self.sts
  129.  
  130.     
  131.     def wait(self):
  132.         '''Wait for and return the exit status of the child process.'''
  133.         if self.sts < 0:
  134.             (pid, sts) = os.waitpid(self.pid, 0)
  135.             if not pid == self.pid:
  136.                 raise AssertionError
  137.             self.sts = sts
  138.         
  139.         return self.sts
  140.  
  141.  
  142.  
  143. class Popen4(Popen3):
  144.     childerr = None
  145.     
  146.     def __init__(self, cmd, bufsize = -1):
  147.         _cleanup()
  148.         self.cmd = cmd
  149.         (p2cread, p2cwrite) = os.pipe()
  150.         (c2pread, c2pwrite) = os.pipe()
  151.         self.pid = os.fork()
  152.         if self.pid == 0:
  153.             os.dup2(p2cread, 0)
  154.             os.dup2(c2pwrite, 1)
  155.             os.dup2(c2pwrite, 2)
  156.             self._run_child(cmd)
  157.         
  158.         os.close(p2cread)
  159.         self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
  160.         os.close(c2pwrite)
  161.         self.fromchild = os.fdopen(c2pread, 'r', bufsize)
  162.  
  163.  
  164. if sys.platform[:3] == 'win' or sys.platform == 'os2emx':
  165.     del Popen3
  166.     del Popen4
  167.     
  168.     def popen2(cmd, bufsize = -1, mode = 't'):
  169.         """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
  170.         be a sequence, in which case arguments will be passed directly to the
  171.         program without shell intervention (as with os.spawnv()). If 'cmd' is a
  172.         string it will be passed to the shell (as with os.system()). If
  173.         'bufsize' is specified, it sets the buffer size for the I/O pipes. The
  174.         file objects (child_stdout, child_stdin) are returned."""
  175.         (w, r) = os.popen2(cmd, mode, bufsize)
  176.         return (r, w)
  177.  
  178.     
  179.     def popen3(cmd, bufsize = -1, mode = 't'):
  180.         """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
  181.         be a sequence, in which case arguments will be passed directly to the
  182.         program without shell intervention (as with os.spawnv()). If 'cmd' is a
  183.         string it will be passed to the shell (as with os.system()). If
  184.         'bufsize' is specified, it sets the buffer size for the I/O pipes. The
  185.         file objects (child_stdout, child_stdin, child_stderr) are returned."""
  186.         (w, r, e) = os.popen3(cmd, mode, bufsize)
  187.         return (r, w, e)
  188.  
  189.     
  190.     def popen4(cmd, bufsize = -1, mode = 't'):
  191.         """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
  192.         be a sequence, in which case arguments will be passed directly to the
  193.         program without shell intervention (as with os.spawnv()). If 'cmd' is a
  194.         string it will be passed to the shell (as with os.system()). If
  195.         'bufsize' is specified, it sets the buffer size for the I/O pipes. The
  196.         file objects (child_stdout_stderr, child_stdin) are returned."""
  197.         (w, r) = os.popen4(cmd, mode, bufsize)
  198.         return (r, w)
  199.  
  200. else:
  201.     
  202.     def popen2(cmd, bufsize = -1, mode = 't'):
  203.         """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
  204.         be a sequence, in which case arguments will be passed directly to the
  205.         program without shell intervention (as with os.spawnv()). If 'cmd' is a
  206.         string it will be passed to the shell (as with os.system()). If
  207.         'bufsize' is specified, it sets the buffer size for the I/O pipes. The
  208.         file objects (child_stdout, child_stdin) are returned."""
  209.         inst = Popen3(cmd, False, bufsize)
  210.         return (inst.fromchild, inst.tochild)
  211.  
  212.     
  213.     def popen3(cmd, bufsize = -1, mode = 't'):
  214.         """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
  215.         be a sequence, in which case arguments will be passed directly to the
  216.         program without shell intervention (as with os.spawnv()). If 'cmd' is a
  217.         string it will be passed to the shell (as with os.system()). If
  218.         'bufsize' is specified, it sets the buffer size for the I/O pipes. The
  219.         file objects (child_stdout, child_stdin, child_stderr) are returned."""
  220.         inst = Popen3(cmd, True, bufsize)
  221.         return (inst.fromchild, inst.tochild, inst.childerr)
  222.  
  223.     
  224.     def popen4(cmd, bufsize = -1, mode = 't'):
  225.         """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
  226.         be a sequence, in which case arguments will be passed directly to the
  227.         program without shell intervention (as with os.spawnv()). If 'cmd' is a
  228.         string it will be passed to the shell (as with os.system()). If
  229.         'bufsize' is specified, it sets the buffer size for the I/O pipes. The
  230.         file objects (child_stdout_stderr, child_stdin) are returned."""
  231.         inst = Popen4(cmd, bufsize)
  232.         return (inst.fromchild, inst.tochild)
  233.  
  234.     __all__.extend([
  235.         'Popen3',
  236.         'Popen4'])
  237.